
export const init = (self, tiyData) => {

    tiyData.tPage.stepNumber=40;
     tiyData.tPage.currentPage=1;
    tiyData.tParam.countryName='';
     tiyData.tParam.method='';
    tiyData.tParam.isNotConfig=false;
}

export const webList = (self, tiyData,type) => {
  self.$Spin.show();
  let params={};
  if(type==1){
    params={
      "pageSize": tiyData.tPage.stepNumber,
      "pageNum": tiyData.tPage.currentPage,
      "countryName": tiyData.tParam.countryName,
      "method": tiyData.tParam.method,
      "isNotConfig": tiyData.tParam.isNotConfig
    }
  }else{
    params={
      "pageSize": 200,
      "pageNum": 1,
      "countryName": tiyData.tParam.countryName,
      "method": '',
      "isNotConfig": false,
    }
  }

  self.$apiOther.getCountryPostageList(params)
    .then(function (response) {
      self.$Spin.hide();
      if (response.success) {
        tiyData.tData.countryPostageList.length = 0;
        let postagePage = response.result;
        tiyData.tPage.total = postagePage.total;
        tiyData.tPage.stepNumber = postagePage.pageSize;
        if(type==1){
          tiyData.tData.countryPostageList.splice(0,tiyData.tData.countryPostageList.length );
          postagePage.list.forEach(
            function (item) {
              tiyData.tData.countryPostageList.push(item);
            }
          );
        }
        if(type==2){
          postagePage.list.forEach(
            function (item) {
              tiyData.tData.countryPostageSortList.push(item);
            }
          );
        }

        if (0 == tiyData.tData.countryPostageList.length&&type==1) {
          self.$Message.error("查询没有结果");
        }
      } else {
        self.$Spin.hide();
        self.$Message.error(response.errorMessage);
      }
    });

};


export const actionDelete=(self,tiyData)=>{
  var index = tiyData.tStatus.actionModal.operationIndex;
  var postageId = tiyData.tData.countryPostageList[index].postageId;
  var countryId = tiyData.tData.countryPostageList[index].countryId;
  self.$Spin.show();
  self.$apiOther.deleteCountryPostage({
    "postageId":postageId,
    "countryId":countryId,
  }).then(function (response) {
    self.$Spin.hide();
    if(response.success){
      self.$Message.success("操作成功!");
      webList(self,tiyData,1);
    }else{
      self.$Spin.hide();
      self.$Message.error(response.errorMessage);
    }

  })

}

export const showPostageDetail = (self,tiyData) =>{
    self.$Spin.show();
    var selectCountry = tiyData.tData.countryPostageList[tiyData.tStatus.showPostageDetailModal.operationIndex];
    self.$apiOther.getPostage({
      "postageId":selectCountry.postageId,
      "withCounties": false
    }).then(function (response) {
      self.$Spin.hide();
      if(response.success){
        let postage = response.result;
        tiyData.tData.postage.id = postage.id;
        tiyData.tData.postage.name = postage.name;
        tiyData.tData.postage.method = "尚未配置";
        self.$store.getters.dictionaries.postageMethodDict.forEach(
          function(item){
            if(item.value === postage.method ){
              tiyData.tData.postage.method  = item.name;
              return;
            }
          }
        );
        tiyData.tData.postage.minTime = postage.minTime;
        tiyData.tData.postage.maxTime = postage.maxTime;
        tiyData.tData.postage.note = postage.note;
        tiyData.tData.postage.innerNote = postage.innerNote;
        tiyData.tData.postage.minWeight = postage.minWeight;
        tiyData.tData.postage.postageRules = [];
        postage.postageRules.forEach(function (item) {
          tiyData.tData.postage.postageRules.push(item);
        });
        tiyData.tData.postage.postageRules = postage.postageRules;
        tiyData.tStatus.showPostageDetailModal.title = selectCountry.countryCn + "运费详请";
        tiyData.tStatus.showPostageDetailModal.isShow = true;
      }else{
        self.$Spin.hide();
        self.$Message.error(response.errorMessage);
      }

    })
};

/**
 * 国家运费排序
 * @param self
 * @param tiyData
 */
export const countrySort= (self,tiyData) => {
  self.$Spin.show();
  let countrySequenceList=new Array()
  if(tiyData.tData.countryPostageSortList.length > 0){
    tiyData.tData.countryPostageSortList.forEach(function(item,index){
      countrySequenceList.push({
        'id':item.postageId+","+item.countryId,
        'sequence':index
      })
    })
    self.$apiOther.batchSequenceCountry({
      "countrySequenceList":countrySequenceList
    }).then(function (response) {
      self.$Spin.hide();
      if (response.success) {
        self.$Modal.success({content:'排序成功!'});
      } else {
        self.$Modal.error({content:response.errorMessage});
      }
    });
  }
}
